package com.neusoft.ruidaozaixian.controller.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.neusoft.ruidaozaixian.R;
import com.neusoft.ruidaozaixian.controller.activity.LoginActivity;
import com.neusoft.ruidaozaixian.controller.activity.PostActivity;
import com.neusoft.ruidaozaixian.controller.adapter.PostCourseAdapter;
import com.neusoft.ruidaozaixian.model.bean.PostCourse;
import com.neusoft.ruidaozaixian.util.baseinfo.Connectinfo;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;


/**
 * A simple {@link Fragment} subclass.
 */
public class PostCourseFragment extends Fragment {
    private ListView listView;
    private PostCourse.IndexPositionListBean bean;
    private ArrayList<PostCourse.IndexPositionListBean> beanList;
    private PostCourseAdapter postCourseAdapter;
    private List<PostCourse.IndexPositionListBean> indexPosition;
    public static String POST_ID;
    public PostCourseFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_post_course, container, false);

        listView= (ListView)view.findViewById(R.id.listview);

        getDate();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String postId = ((TextView)view. findViewById(R.id.postId)).getText().toString();
                POST_ID=postId;
                Intent intent = new Intent(getActivity(), PostActivity.class);
                startActivity(intent);

            }
        });
        return view;
    }


    public void getDate() {
        //创建一个Request
        /*FormBody formBody = new FormBody.Builder()
                .add("postid", "60")
                .build();*/
        final Request request = new Request.Builder()
                .addHeader("cookie", LoginActivity.s)
                .url(Connectinfo.obtainPosition)
                        //.post(formBody)
                .build();
        //new call
        Call call = LoginActivity.mOkHttpClient.newCall(request);
        //请求加入调度
        call.enqueue(new Callback() {
            //失败回掉
            @Override
            public void onFailure(Call call, IOException e) {

            }

            //成功回掉
            @Override
            public void onResponse(Call call, Response response) throws IOException {


                beanList = new ArrayList<PostCourse.IndexPositionListBean>();
                if (response.isSuccessful()) {

                    Message msg=Message.obtain();
                    msg.obj=response.body().string();
                    handler.sendMessage(msg);//将msg发送过去


                }
            }

        });
    }
    private Handler handler = new Handler(){
        public void handleMessage(Message msg) {
            String res= (String) msg.obj;
            Gson gson=new Gson();
            PostCourse postCourse = gson.fromJson(res, PostCourse.class);
            indexPosition = postCourse.getIndexPositionList();

            for (PostCourse.IndexPositionListBean p:indexPosition){
                bean= new PostCourse.IndexPositionListBean();
                bean.setPostId(p.getPostId());
                bean.setPostName(p.getPostName());
                bean.setPersonNums(p.getPersonNums());
                bean.setCourseHours(p.getCourseHours());
                bean.setCourseNums(p.getCourseNums());
                bean.setPostUrl(p.getPostUrl());
                beanList.add(bean);
                postCourseAdapter = new PostCourseAdapter(PostCourseFragment.this.getActivity(),beanList);


                listView.setAdapter(postCourseAdapter);
            }

        }

    };
}
